package com.mulanweb.utils;

import com.jfinal.core.Const;
import com.jfinal.kit.StrKit;
import com.mulanweb.db.common.Constant;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * 编码工具类<br/>
 * 1.将byte[]转为各种进制的字符串<br/>
 * 2.basic 64 encode<br/>
 * 3.basic 64 decode<br/>
 * 4.获取byte[]的md5值<br/>
 * 5.获取字符串md5值<br/>
 * 6.结合base64实现md5加密<br/>
 * 7.AES加密<br/>
 * 8.AES加密为base 64 code<br/>
 * 9.AES解密<br/>
 * 10.将base 64 code AES解密<br/>
 * Created by hcy on 2016/4/9.
 */
public class AseUtils {

   /* public static void main(String[] args) throws Exception {
        String content = "我爱你";
        System.out.println("加密前：" + content);

        String key = "123456";
        System.out.println("加密密钥和解密密钥：" + key);

        String encrypt = aesEncrypt(content, key);
        System.out.println("加密后：" + encrypt);

        String decrypt = aesDecrypt(encrypt, key);
        System.out.println("解密后：" + decrypt);

        System.out.println("---------"+aesDecrypt("Ghbtm4KEwsoJ3F41lBoRoA==", Constant.XinXiLan.ORDER_AES_KEY));
    }*/

    /**
     * 将byte[]转为各种进制的字符串
     *
     * @param bytes byte[]
     * @param radix 可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
     * @return 转换后的字符串
     */
    private static String binary(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }


    /**
     * 结合base64实现md5加密
     *
     * @param msg 待加密字符串
     * @return 获取md5后转为base64
     * @throws Exception
     */
    private static String md5Encrypt(String msg) throws Exception {
        return StrKit.isBlank(msg) ? null : Base64.encodeBase64String(DigestUtils.md5(msg));
    }

    /**
     * AES加密
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     * @throws Exception
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(encryptKey.getBytes(Const.DEFAULT_ENCODING)));

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }

    /**
     * AES加密为base 64 code
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return Base64.encodeBase64String(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * AES解密
     *
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey   解密密钥
     * @return 解密后的String
     * @throws Exception
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(decryptKey.getBytes(Const.DEFAULT_ENCODING)));

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);

        return new String(decryptBytes, Const.DEFAULT_ENCODING);
    }

    /**
     * 将base 64 code AES解密
     *
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StrKit.isBlank(encryptStr) ? null : aesDecryptByBytes(Base64.decodeBase64(encryptStr), decryptKey);
    }

}
